package com.cole.gx095.controller;

import com.cole.gx095.Repository.*;
import com.cole.gx095.controller.utils.CommonRep;
import com.cole.gx095.domain.CostVo;
import com.cole.gx095.domain.mapper.CostVOMapper;
import com.cole.gx095.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/check")
@Transactional
@CrossOrigin
public class checkController {
    @Autowired
    SalecontractRepository salecontractRepository;
    @Autowired
    SalebillRepository salebillRepository;
    @Autowired
    SaleBillgoodsRepository saleBillgoodsRepository;
    @Autowired
    CorrelationRepository correlationRepository;
    @Autowired
    PurchasecontractRepository purchasecontractRepository;
    @Autowired
    PurchasebillRepository purchasebillRepository;
    @GetMapping("/List")
    CommonRep List(Integer currentPage, Integer pageSize){
        Page<Salecontract> page1 = salecontractRepository.findAll(PageRequest.of(currentPage - 1, pageSize));
        for (Salecontract salecontract : page1.getContent()) {
            List<CostVo> costVos = new ArrayList<>();
            List<Salebill> salebillList = salebillRepository.findAllBycontractNum(salecontract.getContractNum());
            for (Salebill salebill : salebillList) {
                List<SaleBillgoods> saleBillgoodsList = saleBillgoodsRepository.findByBillId(salebill.getId());
                for (SaleBillgoods saleBillgoods : saleBillgoodsList) {
                    CostVo conver = CostVOMapper.INSTANCT.conver(salebill);
                    conver.setContractNum("");
                    conver.setTypeName(saleBillgoods.getTypeName());
                    conver.setGoodsName(saleBillgoods.getGoodsName());
                    conver.setModle(saleBillgoods.getModle());
                    conver.setGoodsUnitPrice(saleBillgoods.getGoodsUnitPrice());
                    conver.setGoodsAccount(saleBillgoods.getGoodsAccount());
                    costVos.add(conver);
                }
            }
            //补充关联表信息
            List<Correlation> correlationList = correlationRepository.findByConId(salecontract.getId());
            for (Correlation correlation : correlationList) {
                CostVo costVo = new CostVo();
                costVo.setContractNum(correlation.getPurchanseContractNum());
                //此处作供应商使用
                costVo.setBuyUnit(purchasecontractRepository.getById(correlation.getPurchanseContractId()).getSupplier());
                costVo.setBillNum(correlation.getPurchanseBillNum());
                Purchasebill purchasebill = purchasebillRepository.getById(correlation.getPurchanseBillId());
                costVo.setBillDate(purchasebill.getBillDate());
                costVo.setBillType(purchasebill.getBillType());
                costVo.setBillAccount(purchasebill.getBillAccount());

                costVo.setTypeName(correlation.getTypeName());
                costVo.setGoodsName(correlation.getGoodsName());
                costVo.setModle(correlation.getModle());
                costVo.setGoodsUnitPrice(correlation.getGoodsUnitPrice());
                costVo.setGoodsAccount(correlation.getCorrelationAccount());
                costVo.setCreateAdminTrueName(correlation.getCreateAdminTrueName());
                costVo.setCreateDate(correlation.getCreateDate());
                if(correlation.getStatus() == 0)
                    costVos.add(costVo);
            }
            salecontract.setCostVoList(costVos);
        }
        return CommonRep.ok()
                .data("page",page1);
    }
}
